package com.example.lib_cache.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.model.LatLng;
import com.example.lib_cache.model.Address;
import com.example.lib_cache.model.EndPoi;
import com.example.lib_cache.model.Navigation;
import com.example.lib_cache.model.Poi;
import com.example.lib_common.util.LiveDataManager;
import com.example.lib_common.util.MileageUtils;
import com.example.lib_common.util.TimeUtils;

import java.util.Date;

public class MapFragmentViewModel extends ViewModel {

    MutableLiveData<Address> addressMutableLiveData;

    MutableLiveData<Poi> poiMutableLiveData;

    MutableLiveData<EndPoi> endPoiMutableLiveData;

    Navigation settleNavigation = null;
    String distance = "";
    String duration = "";
    String distanceUnit = "";
    MutableLiveData<Boolean> isTransform = new MutableLiveData<>();

    public String getDistance() {
        return distance;
    }

    public String getDuration() {
        return duration;
    }

    public String getDistanceUnit() {
        return distanceUnit;
    }

    public MutableLiveData<Boolean> getIsTransform() {
        return isTransform;
    }

    public void setIsTransform(Boolean isTransform) {
        this.isTransform.postValue(isTransform);
    }

    public MutableLiveData<Address> getAddressMutableLiveData(){
        return addressMutableLiveData;
    }

    public void setAddressMutableLiveData(Address address  ){
        this.addressMutableLiveData = LiveDataManager
                .getInstance().with("address", Address.class);
        this.addressMutableLiveData.postValue(address);
    }

    public MutableLiveData<EndPoi> getEndPoiMutableLiveData() {
        return endPoiMutableLiveData;
    }

    public void setEndPoiMutableLiveData(EndPoi poi) {
        this.endPoiMutableLiveData = new MutableLiveData<>();
        this.endPoiMutableLiveData.setValue(poi);
    }

    public MutableLiveData<Poi> getPoiMutableLiveData() {
        return poiMutableLiveData;
    }

    public void setPoiMutableLiveData(Poi poi) {
        this.poiMutableLiveData = new MutableLiveData<>();
        this.poiMutableLiveData.setValue(poi);
    }


    public void refreshLocation(BDLocation location) {
        Address address = new Address();
        address.setLocationDescribe(location.getLocationDescribe());
        address.setCity(location.getCity());
        address.setCountry(location.getCountry());
        address.setDistrict(location.getDistrict());
        address.setProvince(location.getProvince());
        address.setTown(location.getTown());
        setAddressMutableLiveData(address);
    }

    public void getPoiInfo(){
        setPoiMutableLiveData(LiveDataManager
                .getInstance().with("PoiInfo",Poi.class)
                .getValue());
    }

    public void getEndPoiInfo(){
        setEndPoiMutableLiveData(LiveDataManager
                .getInstance().with("EndPoiInfo",EndPoi.class)
                .getValue());
    }

    public void transformTime(String lastTime,String thisTime) {
        duration = TimeUtils.TimeCompareToHour(lastTime,thisTime);
        settleNavigation =  LiveDataManager
                .getInstance().with("settleNav", Navigation.class).getValue();
        distance = MileageUtils.meterToKilometersWithoutUnit(
                settleNavigation.getDistance());
        if(settleNavigation.getDistance()>1000){
            distanceUnit = "公里";
        }else {
            distanceUnit = "米";
        }
        setIsTransform(true);
    }
}
